/**
 * 
 */
package repository.relsys;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

import java.util.Map;

import org.apache.log4j.Logger;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

import repository.relsys.app.Settings;
import repository.relsys.config.Property;
import repository.relsys.domainmodel.general.Resource;
import repository.relsys.domainmodel.ltool.interfaces.Blog;
import repository.relsys.domainmodel.ltool.interfaces.CollaborativeTagging;
import repository.relsys.domainmodel.ltool.interfaces.ContactList;
import repository.relsys.domainmodel.ltool.interfaces.Feature;
import repository.relsys.domainmodel.ltool.interfaces.GeneralInformation;
import repository.relsys.domainmodel.ltool.interfaces.InstantMessaging;
import repository.relsys.domainmodel.ltool.interfaces.LearningTool;
import repository.relsys.domainmodel.ltool.interfaces.MobileDevice;
import repository.relsys.domainmodel.ltool.interfaces.MultimediaHosting;
import repository.relsys.domainmodel.ltool.interfaces.OperetingSystem;
import repository.relsys.domainmodel.ltool.interfaces.SiteSearch;
import repository.relsys.domainmodel.ltool.interfaces.SocialNetworking;
import repository.relsys.domainmodel.ltool.interfaces.TechnicalInformation;
import repository.relsys.domainmodel.ltool.interfaces.ToolAvailability;
import repository.relsys.domainmodel.ltool.interfaces.ToolCost;
import repository.relsys.domainmodel.ltool.interfaces.UserProfileManagement;
import repository.relsys.domainmodel.ltool.pojos.LearningToolCls;
import repository.relsys.domainmodel.ltool.pojos.MobileDeviceCls;
import repository.relsys.domainmodel.ltool.pojos.OperetingSystemCls;
import repository.relsys.domainmodel.ltool.pojos.ToolAvailabilityCls;
import repository.relsys.domainmodel.ltool.pojos.ToolCostCls;

import repository.relsys.services.DataModelManager;

import repository.relsys.services.RelsysReadWrite;
import repository.relsys.services.datacentral.FileDataProvider;
import repository.relsys.services.spring.RelsysResourceFactory;
import repository.relsys.util.json.JSONExporter;
import repository.relsys.util.json.JSONExporterCls;
import thewebsemantic.RDF2Bean;

public class RelsysTest2 {

	private static Logger logger = Logger
			.getLogger(RelsysTest2.class.getName());

	public static void main(String[] args) {
		try {

			Settings.getInstance();

			RelsysTest2.initRepository();
			logger.info("Repository initialised!");

			System.in.read();

			DataModelManager.getInstance().flush();

		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}

	public static void initRepository() throws Exception {

		RelsysReadWrite rrw = new RelsysReadWrite();

		Collection<LearningTool> ltools = new LinkedList<LearningTool>();

		RelsysResourceFactory rf = new RelsysResourceFactory();

		LearningTool ltool1 = (LearningTool) rf
				.createResource(LearningTool.class);
		ltool1.setTitle("WordPress");
		ltool1
				.setToolImgURL("http://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Wordpress-logo.png/250px-Wordpress-logo.png");

		GeneralInformation gi1 = (GeneralInformation) rf
				.createResource(GeneralInformation.class);

		gi1
				.setDescription("WordPress is an open source blogging platform that is free and easy to use.");
		gi1.setToolURL("http://www.wordpress.com");
		ToolCost tc = (ToolCost) rf.createResource(ToolCost.class);
		ToolAvailability ta = (ToolAvailability) rf
				.createResource(ToolAvailability.class);
		tc.setTitle("Free");
		ta.setTitle("Hosted");

		gi1.setToolCost(tc);
		gi1.setToolAvailability(ta);

		ltool1.setGeneralInfo(gi1);

		TechnicalInformation ti1 = (TechnicalInformation) rf
				.createResource(TechnicalInformation.class);
		ti1.registration(true);
		ti1.setLaunchedTime("12-01-2001");
		ti1.setVersion("2.9");

		ti1.isOpenSource(false);

		MobileDevice mdCls1 = (MobileDevice) rf
				.createResource(MobileDevice.class);
		mdCls1.setTitle("Blackberry");

		MobileDevice mdCls2 = (MobileDevice) rf
				.createResource(MobileDevice.class);
		mdCls2.setTitle("IPhone");
		MobileDevice mdCls3 = (MobileDevice) rf
				.createResource(MobileDevice.class);
		mdCls3.setTitle("Andorid");
		ti1.addMD(mdCls1);
		ti1.addMD(mdCls2);
		ti1.addMD(mdCls3);

		OperetingSystem os1 = (OperetingSystem) rf
				.createResource(OperetingSystem.class);
		os1.setTitle("Linux");
		OperetingSystem os2 = (OperetingSystem) rf
				.createResource(OperetingSystem.class);
		os2.setTitle("windows");
		OperetingSystem os3 = (OperetingSystem) rf
				.createResource(OperetingSystem.class);
		os3.setTitle("MacOS");

		ti1.addOS(os1);
		ti1.addOS(os2);
		ti1.addOS(os3);
		/*
		 * ti1.addMD(MobileDeviceCls.BLACKBERRY);
		 * ti1.addMD(MobileDeviceCls.ANDROID);
		 * ti1.addMD(MobileDeviceCls.IPHONE);
		 * ti1.addOS(OperetingSystemCls.LINUX);
		 * ti1.addOS(OperetingSystemCls.WINDOWS);
		 * ti1.addOS(OperetingSystemCls.MAC);
		 */

		ltool1.setTechnicalInfo(ti1);

		Collection<Feature> features = new LinkedList<Feature>();
		Blog blog = (Blog) rf.createResource(Blog.class);
		ContactList folowers = (ContactList) rf
				.createResource(ContactList.class);
		blog.setTitle("Blog");
		folowers.setTitle("ContactList");
		features.add(blog);
		features.add(folowers);

		ltool1.setFeatures(features);

		LearningTool ltool2 = (LearningTool) rf
				.createResource(LearningTool.class);
		ltool2.setTitle("Facebook");
		ltool2
				.setToolImgURL("http://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Facebook.svg/230px-Facebook.svg.png");

		GeneralInformation gi2 = (GeneralInformation) rf
				.createResource(GeneralInformation.class);

		gi2
				.setDescription("Facebook is a leading social networking site with over 300 million members. There are now many Facebook applications that allow it be used productively in education and business");
		gi2.setToolURL("http://www.facebook.com");
		gi2.setToolCost(tc);
		gi2.setToolAvailability(ta);

		ltool2.setGeneralInfo(gi2);

		TechnicalInformation ti2 = (TechnicalInformation) rf
				.createResource(TechnicalInformation.class);
		ti2.registration(true);
		ti2.setLaunchedTime("12-01-2005");
		ti2.setVersion("3.9");

		ti2.isOpenSource(false);

		ti2.addMD(mdCls1);
		ti2.addMD(mdCls2);
		ti2.addMD(mdCls3);

		ti2.addOS(os1);
		ti2.addOS(os2);
		ti2.addOS(os3);

		ltool2.setTechnicalInfo(ti2);

		Collection<Feature> facefeatures = new LinkedList<Feature>();
		UserProfileManagement upm = (UserProfileManagement) rf
				.createResource(UserProfileManagement.class);
		SocialNetworking sn = (SocialNetworking) rf
				.createResource(SocialNetworking.class);
		SiteSearch ss = (SiteSearch) rf.createResource(SiteSearch.class);
		MultimediaHosting mh = (MultimediaHosting) rf
				.createResource(MultimediaHosting.class);
		InstantMessaging im = (InstantMessaging) rf
				.createResource(InstantMessaging.class);
		CollaborativeTagging ct = (CollaborativeTagging) rf
				.createResource(CollaborativeTagging.class);

		upm.setTitle("UserProfileManagement");
		sn.setTitle("SocialNetworking");
		ss.setTitle("SiteSearch");
		mh.setTitle("MultimediaHosting");
		im.setTitle("InstantMessaging");
		ct.setTitle("CollaborativeTagging");

		facefeatures.add(upm);
		facefeatures.add(sn);
		facefeatures.add(ss);
		facefeatures.add(mh);
		facefeatures.add(im);
		facefeatures.add(ct);
		ltool2.setFeatures(facefeatures);

		ltools.add(ltool1);
		ltools.add(ltool2);

		// Collection<LearningTool> ltools =importLT("LearningTools.rdf",
		// "RDF/XML");

		rrw.saveResources(ltools);

		Collection<LearningTool> learningtools = rrw
				.loadResourcesFromDB(LearningTool.class);
		LinkedList<LearningTool> list = (LinkedList<LearningTool>) learningtools;

		Iterator<LearningTool> iter = list.iterator();

		while (iter.hasNext()) {
			LearningTool t = (LearningTool) iter.next();

			System.out.println(t.getToolImgURL());

		}

		try {
			JSONExporter jsonexp = new JSONExporterCls();
			System.out.println(jsonexp.convertLTToJSON(learningtools));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@SuppressWarnings("unchecked")
	public static Collection<LearningTool> importLT(String fileName,
			String syntax) {
		RelsysResourceFactory rf = new RelsysResourceFactory();

		// OntModel dataModel = ModelFactory.createOntologyModel();
		Model dataModel = null;
		try {
			dataModel = ModelFactory.createDefaultModel();
			dataModel.setNsPrefix("lt",
					"http://diplomski.nelakolundzija.org/LTontology.rdf#");
			dataModel.setNsPrefix("swc",
					"http://data.semanticweb.org/ns/swc/ontology#");
			dataModel.setNsPrefix("jenabean", "http://thewebsemantic.com/");
			dataModel.setNsPrefix("dc", "http://purl.org/dc/elements/1.1/");
			dataModel.setNsPrefix("xsd", "http://www.w3.org/2001/XMLSchema#");
			dataModel.setNsPrefix("rdfs",
					"http://www.w3.org/2000/01/rdf-schema#");

			dataModel.read(new FileInputStream(fileName),
					"http://diplomski.nelakolundzija.org/LTontology.rdf#",
					syntax);
		} catch (Exception e) {
			logger.error("Could not load DataModel from file: " + e);
		}

		/*
		 * dataModel.read("http://diplomski.nelakolundzija.org/individuals.owl",
		 * "RDF/XML");
		 * 
		 * ExtendedIterator<OntClass> it= dataModel.listClasses();
		 * while(it.hasNext()){ OntClass cls = it.next();
		 * System.out.println(cls.getURI()); }
		 */

		RDF2Bean reader = new RDF2Bean(dataModel);
		Collection<LearningTool> ltools = reader.loadDeep(rf
				.getBeanImplementationClass(LearningTool.class));

		LinkedList<LearningTool> list = (LinkedList<LearningTool>) ltools;

		Iterator<LearningTool> iter = list.iterator();

		while (iter.hasNext()) {
			LearningTool t = (LearningTool) iter.next();
			System.out.println(t.getTitle());
		}
		return ltools;

	}

}
